
#-c --cpu Cortex-M3 -g -O0 --apcs=interwork --split_sections -I ../../SRC/StdPeriphDriver/inc -I ../../SRC/CMSIS -I ../../SRC/Debug -I ../GPIO_Toggle
#-I./RTE/_CH32F20x
#-ID:/SofWareInstall/Keil_Package/Keil/WCH32F2xx_DFP/1.0.2/Device/Include
#-ID:/SofWareInstall/Keil/ARM/CMSIS/Include
#-D__UVISION_VERSION="527"
#-o .\obj\*.o --omf_browse .\obj\*.crf --depend .\obj\*.d

#--cpu Cortex-M3 -g --apcs=interwork
#-I .\RTE\_CH32F20x
#-I D:\SofWareInstall\Keil_Package\Keil\WCH32F2xx_DFP\1.0.2\Device\Include
#-I D:\SofWareInstall\Keil\ARM\CMSIS\Include
#--pd "__UVISION_VERSION SETA 527" --list ".\list\*.lst" --xref -o "*.o" --depend "*.d"

#--cpu Cortex-M3 *.o
#--strict --scatter ".\obj\CH32F20x.sct"
#--summary_stderr --info summarysizes --map --xref --callgraph --symbols
#--info sizes --info totals --info unused --info veneers
#--list ".\list\CH32F20x.map"
#-o .\obj\CH32F20x.axf

#设置CMake最低支持版本
cmake_minimum_required(VERSION 3.17)


#set(KEIL_PATH D:/SofWareInstall/Keil/ARM/ARMCC/bin)
set(ARM_NONE_GCC E:/softWareInstall/arm_none_gcc/bin)

#交叉编译：设置目标机器类型
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR cortex-m3)


#设置编译器
set(CMAKE_C_COMPILER ${ARM_NONE_GCC}/arm-none-eabi-gcc.exe)
set(CMAKE_CXX_COMPILER ${ARM_NONE_GCC}/arm-none-eabi-g++.exe)
#set(CMAKE_ASM_COMPILER ${ARM_NONE_GCC}/arm-none-eabi-as.exe)
set(CMAKE_C_COMPILER_WORKS TRUE)
set(CMAKE_CXX_COMPILER_WORKS TRUE)
set(CMAKE_ASM_COMPILER_WORKS TRUE)
#设置链接器
set(CMAKE_C_LINK_EXECUTABLE ${ARM_NONE_GCC}/arm-none-eabi-ld.exe)
set(CMAKE_CXX_LINK_EXECUTABLE ${ARM_NONE_GCC}/arm-none-eabi-ld.exe)
set(CMAKE_ASM_LINK_EXECUTABLE ${ARM_NONE_GCC}/arm-none-eabi-ld.exe)


#接口：用户变量
set(FPU FALSE CACHE STRING "global") #设置默认FPU选项

#接口: FPU使能控制
function(CMSIS_EnableFPU isEnabled)
    set(FPU ${isEnabled} CACHE STRING "global")
endfunction()

#接口：设置Sections脚本
function(CMSIS_SetSectionsScriptPath path)
    set(SECTIONS_SCRIPT_PATH ${path} CACHE STRING "global")
endfunction()

#接口：配置交叉编译
function(CMSIS_CrossCompilingConfiguration)

    #设置变量
#    set(C_TARGET_FLAG --target=arm-arm-none-eabi)
#    set(ASM_TARGET_FLAG --target=arm-arm-none-eabi)
#    set(LINKER_TARGET_FLAG --cpu=Cortex-M4.fp.sp)
    set(C_TARGET_FLAG --target=arm-arm-none-eabi)
    set(ASM_TARGET_FLAG  -march=cortex-m3)
    set(LINKER_TARGET_FLAG -mcpu=cortex-m3 -march=cortex-m3)
    #设置FPU,Target参数
#    if (FPU)
#        set(FPU_FLAG "-mfloat-abi=hard -mfpu=fpv4-sp-d16 -D__FPU_PRESENT")
#    else()
#        set(FPU_FLAG "-mfloat-abi=soft -mfpu=none" )
#        set(ASM_TARGET_FLAG --target=armv7em-arm-none-eabi)
#        set(LINKER_TARGET_FLAG "--cpu=Cortex-M4 --fpu=SoftVFP")
#    endif()

    #设置通用编译参数
    #set(COMPILE_RULE_FLAG "-mcpu=${CMAKE_SYSTEM_PROCESSOR} ${FPU_FLAG}")

    #设置C编译器选项
    set(CMAKE_C_FLAGS_INIT "-mthumb -mcpu=cortex-m3 -Wa,--warn -g -O0 -fomit-frame-pointer " CACHE STRING "global")

    #设置C++编译器选项
    set(CMAKE_CXX_FLAGS_INIT ${CMAKE_C_FLAGS_INIT} CACHE STRING "global")

    #设置ASM编译器选项
    set(CMAKE_ASM_FLAGS_INIT " ${ASM_TARGET_FLAG}" CACHE STRING "global")

    #判断链接脚本是否存在
    if (NOT SECTIONS_SCRIPT_PATH)
        message(FATAL_ERROR "You not set SECTIONS_SCRIPT_PATH!")
    endif ()

    #设置链接选项
    #--cpu Cortex-M3 *.o
    ##--strict --scatter ".\obj\CH32F20x.sct"
    ##--summary_stderr --info summarysizes --map --xref --callgraph --symbols
    ##--info sizes --info totals --info unused --info veneers
    ##--list ".\list\CH32F20x.map"
    ##-o .\obj\CH32F20x.axf
    set(CMAKE_EXE_LINKER_FLAGS_INIT " " CACHE STRING "global")

    #使能汇编
    enable_language(ASM)

endfunction()
